﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class CS_JCCYY_Edit : System.Web.UI.Page
    {
        protected string id;
        protected CS_JCCYY entity;
        protected string resultMessage = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"];

            if (!IsPostBack)
            {
                if (id == null)
                {
                    this.lblTitle.Text = "新增进出仓原因";
                }
                else
                {
                    this.lblTitle.Text = "修改进出仓原因";
                    CS_JCCYYSet_Dao dao = new CS_JCCYYSet_Dao();
                    entity = dao.Query(id);
                    setFields();
                    this.tbCode.Enabled = false;
                }
            }
        }

        private void add()
        {
            entity = new CS_JCCYY();
            getFields();

            CS_JCCYYSet_Dao dao = new CS_JCCYYSet_Dao();

            try
            {
                if (dao.Exist(entity.BH, entity.MC))
                {
                    resultMessage = "同样编号或名称的进出仓原因已存在";
                    return;
                }

                int result = dao.Add(entity);
                if (result == 1)
                {
                    resultMessage = "添加进出仓原因成功";
                }
            }
            catch (Exception ex)
            {
                resultMessage = "添加进出仓原因失败:" + ex.Message;
            }
        }

        private void update()
        {
            entity = new CS_JCCYY();
            getFields();
            CS_JCCYYSet_Dao dao = new CS_JCCYYSet_Dao();

            try
            {
                //if (dao.Exist(entity.BH, entity.MC))
                //{
                //    resultMessage = "同样编号或名称的进出仓原因已存在";
                //    return;
                //}

                int result = dao.Update(entity);
                if (result == 1)
                {
                    resultMessage = "更新进出仓原因成功";
                }
            }
            catch (Exception ex)
            {
                resultMessage = "更新进出仓原因失败:" + ex.Message;
            }
        }

        private void setFields()
        {
            this.tbCode.Text = entity.BH;
            this.tbName.Text = entity.MC;
        }

        private void getFields()
        {
            entity.BH = this.tbCode.Text;
            entity.MC = this.tbName.Text;
        }

        private void ClearControlsValue()
        {
            this.tbCode.Text = "";
            this.tbName.Text = "";

            this.lbResult.Text = "";
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            AsyncPostBackTrigger outsideTrigger = new AsyncPostBackTrigger();
            outsideTrigger.ControlID = "btnSave";
            outsideTrigger.EventName = "Click";
            this.UpdatePanel2.Triggers.Add(outsideTrigger);
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            if (id == null)
            {
                add();
            }
            else
            {
                update();
            }

            this.lbResult.Text = resultMessage;
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            if (id == null)
            {
                ClearControlsValue();
            }
            else
            {
                CS_JCCYYSet_Dao dao = new CS_JCCYYSet_Dao();
                entity = dao.Query(id);
                setFields();
            }
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("CS_JCCYY_Query.aspx");
        }
    }
}